# from django.http import JsonResponse
from drf_spectacular.utils import extend_schema
from rest_framework import status
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.mixins import (CreateModelMixin,ListModelMixin,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin)
from rest_framework.viewsets import GenericViewSet

from backend.cews.models import QRMapLocation, QRMapLocationCategory
from utils.autoschema import AutoDocSchema
from utils.decorators import request_data, error_response, check_group_permission

from .serializers import QRMapLocationSerializer, QRMapLocationCategorySerializer

from backend.cews.services.PartsService import generate_qr_with_logo

import base64

class QRMapLocationViewSet(
    GenericViewSet,
    RetrieveModelMixin,
    ListModelMixin,
    UpdateModelMixin,
    CreateModelMixin,
    DestroyModelMixin
):
    serializer_class = QRMapLocationSerializer
    queryset = QRMapLocation.objects.all()

    @extend_schema(responses={200: QRMapLocationSerializer(many=True)})
    @action(detail=False, methods=["post"], url_name="map_location_qrcode",schema=AutoDocSchema())
    @request_data(keys=["qr_string"], types={"qr_string": str})
    @error_response()
    def map_location_qrcode(self,request,qr_string):
        '''
            desc: Get stock code and serial no by qr_string
            
            parameters:
                qr_string:
                    type: string
         
        '''

        data = self.queryset.filter(qr_string=qr_string).first()

        if not data:
            raise ValueError('Location QR code not found')
                          
        serializer = QRMapLocationSerializer(data)
        return Response(status=status.HTTP_200_OK, data={
            "code": 1,
            "data": serializer.data
        })
    

    @extend_schema(responses={200: QRMapLocationSerializer(many=True)})
    @action(detail=False, methods=["post"], url_name="get_location_qrcode",schema=AutoDocSchema())
    @request_data(keys=["id","qrcode_size","cews_size"], types={"id": int,"qrcode_size": int,"cews_size": int})
    @error_response()
    def get_location_qrcode(self,request,id,qrcode_size,cews_size):
        '''
            desc: Get stock code and serial no by qr_string
            
            parameters:
                qr_string:
                    type: string
         
        '''

        qr_loc = self.queryset.get(pk=id)

        qr_text = qr_loc.qr_string
        text = f"CAT: {qr_loc.category},SN: {qr_loc.train}"

        image_data = generate_qr_with_logo(qr_text, text,qrcode_size,cews_size)
        encoded_image = base64.b64encode(image_data).decode('utf-8')
        context = {
            'encoded_image': encoded_image,
            'CAT' : qr_loc.category,
            'SN' : qr_loc.train,
            }

        return Response(status=status.HTTP_200_OK, data={
            "code": 1,
            "data": context
        })
    


 
class QRMapLocationCategoryViewSet(
    GenericViewSet,
    RetrieveModelMixin,
    ListModelMixin,
):
    serializer_class = QRMapLocationCategorySerializer
    queryset = QRMapLocationCategory.objects.all()
